{% extends 'basic_app/master.html' %}
{% block tag_left %}
    {% include 'oratk_app/tag_left_user.html' %}
{% endblock %}
{% block title %}
    <title>ORATK</title>
{% endblock %}
{% block userinfo %}
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <h3 class="page-header">sqlldr 格式化文件</h3>
        <form method="post" enctype="multipart/form-data">
            <label>请上传excel文件：&nbsp </label>
            <div> &nbsp</div>
            <div><input type="file" style="float: left;height: 34px;" name="uploadfile_name" id="file_id">
                <button type="button" class="btn btn-primary" data-loading-text="Loading..." id="upload"> 上传</button>
            </div>
        </form>
    </div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <h3 class="page-header">文件列表</h3>
        <div class="table-responsive">
            {#                <table class="table table-striped table-bordered table-hover">#}
            <table id="sqlaudit-list" data-toggle="table" class="table table-striped table-hover table-bordered"
                   style="table-layout: inherit; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 0px;">
                <thead>
                <tr>
                    <th style="display:none">id</th>
                    <th>文件名</th>
                    <th>文件格式</th>
                    <th>文件大小</th>
                    <th>文件行数</th>
                    <th>工作薄数量</th>
                    <th>文件来源</th>
                    <th>内容format</th>
                    <th>上传时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="tabbody">
                {% for i in current_page %}
                    <tr>
                        <td style="display:none">{{ i.id }}</td>
                        <td>{{ i.file_name|default_if_none:"" }}</td>
                        <td>{{ i.file_mode|default_if_none:"" }}</td>
                        <td>{{ i.file_size|default_if_none:"" }}</td>
                        <td>{{ i.content_count|default_if_none:"" }}</td>
                        <td>{{ i.file_sheets_count|default_if_none:"" }}</td>
                        <td>{{ i.file_make|default_if_none:"" }}</td>
                        <td>{{ i.format_info|default_if_none:"" }}</td>
                        <td>{{ i.rec_date|default_if_none:"" }}</td>
                        <td><a style="cursor:pointer" href="/oratk_app/oratk_most_sqlldr_download?nid={{ i.id }}">下载</a>
                            |
                            <a style="cursor:pointer"
                               href="/oratk_app/oratk_most_sqlldr_delete?delete_tag=sqlldr_file&nid={{ i.id }}"
                               onclick="if(confirm('数据库记录和物理文件同时删除，确认删除吗？')==false)return false;">删除</a></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% include 'basic_app/paginator.html' %}
    </div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <h3 class="page-header">制作sqlldr csv</h3>
        <form method="post" enctype="multipart/form-data">
            <div style="float: left">
                &nbsp 选择文件: &nbsp
                <select class="selectpicker show-tick " style="width:450px" data-live-search="true" name="file_parse"
                        id="file_parse">
                    <option id="def">--请选择文件--</option>
                    {% for i in make_file_result_xlsx %}
                        <option id="a">{{ i.file_name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div id="file_sheet_div" name="file_sheet_div" style="float: left">
                &nbsp 选择sheet: &nbsp
                <select class="selectpicker show-tick " style="width:450px" data-live-search="true" name="sheet_list"
                        id="sheet_list">
                    <option id="def">--请选择sheet--</option>
                </select>
            </div>
            <div>&nbsp</div>
            <h2 class="page-header"></h2>
            <div id="args_detail" class="form-horizontal"></div>
            <div id="tab_detail"></div>
        </form>
    </div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <h3 class="page-header">执行sqlldr</h3>
        <form name="Form" role="search" action="/oratk_app/oratk_select_ora" onsubmit="return validateForm()"
              target="_blank" method="POST">
            {#            <div class="navbar-form navbar-left">#}
            &nbsp 选择实例: &nbsp&nbsp&nbsp&nbsp&nbsp
            {#                <select class="form-control" name="conn_string" id="conn_string">#}
            <select class="selectpicker show-tick " style="width:450px" data-live-search="true" name="conn_string"
                    id="conn_string">
                <option id="def">--请选择实例名--</option>
                {% for i in instanceinfo_result %}
                    <option id="a">{{ i.id }}.{{ i.dbname }}={{ i.conn_string }}</option>
                {% endfor %}
            </select>
            &nbsp 请选择sqlldr模板: &nbsp&nbsp&nbsp&nbsp&nbsp
            {#                <select class="form-control" name="conn_string" id="conn_string">#}
            <select class="selectpicker show-tick " style="width:450px" data-live-search="true" name="control_list"
                    id="control_list">

                <option id="def">--请选择模板--</option>
                {% for i in control_file_list %}
                    <option id="a">{{ i.control_name }}</option>
                {% endfor %}
            </select>
            <div>&nbsp</div>
            <div id="control_file"></div>
        </form>

    </div>
    <div>&nbsp</div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <h3 class="page-header">sqlldr导入记录</h3>
        <div class="table-responsive">
            {#                <table class="table table-striped table-bordered table-hover">#}
            <table id="sqlaudit-list" data-toggle="table" class="table table-striped table-hover table-bordered"
                   style="table-layout: inherit; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 0px;">
                <thead>
                <tr>
                    <th style="display:none">id</th>
                    <th>数据库</th>
                    <th>参数文件</th>
                    <th>log文件</th>
                    <th>bad文件</th>
                    <th>耗时</th>
                    <th>操作时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="tabbody">
                {% for i in current_page_2 %}
                    <tr>
                        <td style="display:none">{{ i.id }}</td>
                        <td>{{ i.ins_info|default_if_none:"" }}</td>
                        <td><a style="cursor:pointer" onclick="edit(this)"
                               val="{{ i.control_file_name }}"> {{ i.control_file_name }}</a></td>
                        <td><a style="cursor:pointer" onclick="edit(this)"
                               val="{{ i.log_file_name }}">{{ i.log_file_name }}</a></td>
                        <td><a style="cursor:pointer" onclick="edit(this)"
                               val="{{ i.bad_file_name }}">{{ i.bad_file_name|default_if_none:"" }}</a></td>
                        <td>{{ i.etime|default_if_none:"" }}</td>
                        <td>{{ i.ctime|default_if_none:"" }}</td>
                        <td><a style="cursor:pointer"
                               href="/oratk_app/oratk_most_sqlldr_delete?delete_tag=sqlldr_record&nid={{ i.id }}"
                               onclick="if(confirm('确认要删除sqlldr执行记录吗？')==false)return false;">删除</a></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% include 'basic_app/paginator_2.html' %}
    </div>

    <!-- 模态框（Modal） 展示文件内容-->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content" id="docModalContent" style="width:600px">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel"></h4>
                </div>
                <span>&nbsp</span>
                <form class="form-horizontal" style="padding-left: 9%">
                    <div class="form-group">
                        <div class="col-sm-10">
                            <textarea type="text" class="form-control" style="height: 400px;width: 500px"
                                      id="file_content"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>


{% endblock %}
{% block script %}
    <script>
        //上传文件
        $('#upload').click(function () {
            is_file = $('#file_id').val();
            if (is_file == null || is_file == "") {
                alert("请选择文件");	//如果为空，请选择文件
                return;
            }
            var file = $('input[type=file]')[0].files[0];
            var formdata = new FormData();
            formdata.append('uploadfile_name', file);
            $.ajax({
                type: "POST",
                data: formdata,
                url: "/oratk_app/oratk_most_sqlldr_ajax", //后台处理函数的url
                cache: false,
                dataType: "html",
                processData: false, // 不处理数据( 必须有)
                contentType: false,  //不设置内容类型 ( 必须要)
                beforeSend: function () {
                    // $('#loadingModal').modal({backdrop: 'static', keyboard: false});
                    showLoading()
                    $('#submit_id').button('loading')
                },
                complete: function () {
                    //$('#loadingModal').modal('hide');
                    hideLoading()
                    $('#submit_id').button('reset')
                },
                success: function (result) {
                    {#$("#args_detail").html(result);#}
                    //location.reload();
                    if (result === 'OK') {
                        location.reload();
                    } else if (result === 'ERROR') {
                        alert('上传失败，请选择正确的文件格式\n' +
                            '仅支持xlsx,txt,csv格式');
                    } else {
                        console.log('500')
                        alert('上传失败，ERROR:\n' + result);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.responseText);
                },
            })
        })
        //sqllldr csv 制作 根绝不同的文件返回不同的sheet
        $('#file_parse').change(function () {
            file_name_check = $('#file_parse').find("option:checked").attr("id");
            if (file_name_check !== 'def') {
                // $("#file_sheet_div").empty()
                $.ajax({
                    type: "POST",
                    data: {
                        'file_name': $('#file_parse').val()
                    },
                    url: "/oratk_app/oratk_most_sqlldr_sheet_ajax", //后台处理函数的url
                    cache: false,
                    dataType: "html",
                    beforeSend: function () {
                        // $('#loadingModal').modal({backdrop: 'static', keyboard: false});
                        showLoading()
                        //$('#submit_id').button('loading')
                    },
                    complete: function () {
                        //$('#loadingModal').modal('hide');
                        hideLoading()
                        //$('#submit_id').button('reset')
                    },
                    success: function (result) {
                        $("#file_sheet_div").html(result);
                        //console.log(result)
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.responseText);
                    },
                })
            } else {
                $("#file_sheet_div").empty()
                $("#file_sheet_div").append('<div style="float: left"> &nbsp 选择sheet: &nbsp</div>  <select class="form-control" name="sheet_list" style="width:300px;" id="sheet_list">\n' +
                    '                        <option id="def">--请选择sheet--</option>\n' +
                    '                    </select>');
            }
        })


        //sqlldr controlfile 展示
        $('#control_list').change(function () {
            control_list_check = $('#control_list').find("option:checked").attr("id");
            if (control_list_check !== 'def') {
                // $("#file_sheet_div").empty()
                $.ajax({
                    type: "POST",
                    data: {
                        'control_name': $('#control_list').val()
                    },
                    url: "/oratk_app/oratk_most_sqlldr_control_file_ajax", //后台处理函数的url
                    cache: false,
                    dataType: "html",
                    beforeSend: function () {
                        // $('#loadingModal').modal({backdrop: 'static', keyboard: false});
                        showLoading()
                        //$('#submit_id').button('loading')
                    },
                    complete: function () {
                        //$('#loadingModal').modal('hide');
                        hideLoading()
                        //$('#submit_id').button('reset')
                    },
                    success: function (result) {
                        $("#control_file").html(result);
                        //console.log(result)
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.responseText);
                    },
                })
            } else {
                $("#control_file").empty()
            }
        })


        //sqlldr 展示文件内容
        function edit(self) {
            var file_name = self.getAttribute('val')
            file_mode = file_name.split('.')[1]
            if (file_mode == 'ctl') {
                var title = 'control文件内容'
            }
            if (file_mode == 'log') {
                var title = 'log文件内容'
            }
            if (file_mode == 'bad') {
                var title = 'bad文件内容'
            }
            var url = "/oratk_app/oratk_most_sqlldr_get_file_content?file_name=" + file_name;
            $.ajax({
                url: url,
                type: "get",
                dataType: "json",
                success: function (res) {
                    $("#file_content").val(res);
                    $('#myModalLabel').html(title)
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.responseText);
                }
            })
            //调用模态框
            $('#myModal').modal('show')
        }


    </script>
{% endblock %}